package trip

import (
	v1 "gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/trip"

	"github.com/gofiber/fiber/v2"
	"github.com/spf13/cast"
)

type TripsController struct {
	v1.BaseController
}

func (controller *TripsController) Index(c *fiber.Ctx) error {

	userId := controller.Auth(c)

	trips := trip.GetTripsByIdWithAll(c.Queries(), userId)

	for index, trip := range trips {
		trips[index].Followed = len(trip.BeFollow) != 0
		trips[index].Booked = len(trip.TripRider) != 0
		trips[index].Owner = trip.UserId == userId
	}

	return controller.Json(c, trips)
}

// 行程详情
func (controller *TripsController) Show(c *fiber.Ctx) error {

	userId := controller.Auth(c)

	tripModel := trip.GetTripByIdWithAll(cast.ToUint64(c.Params("id")), userId)

	tripModel.Followed = len(tripModel.BeFollow) != 0
	tripModel.Booked = len(tripModel.TripRider) != 0
	tripModel.Owner = tripModel.UserId == userId

	if tripModel.ID == 0 {
		return controller.NotFound(c)
	}

	return controller.Json(c, tripModel)
}
